import random
import os
import re
import time

admin=['天一','大空翼','暖哥','jski匪姐','柚子','大菠','赵嘻嘻','陈6岁🏂']
keys=['【装备】','【尺码】','【价格】','【新旧】','【联系】']

def clearmsg():
    f = open('f2hand.txt','w+')
    f.write('')
    f.close()

def addmsg(s,name,qunnum):
    file = open("f2hand.txt",'r')
    lines = file.readlines()
    file.close()
    name=name+' (约车%s群)'%(qunnum)
    f = open('f2hand.txt','a')
    if lines==[]:
        f.write(str(1)+':'+s+'@%s '%(name)+'\n')
    else:
        num=int((lines[-1])[0:1])
        f.write(str(num+1)+':'+s+'@%s '%(name)+'\n')
    f.close()

def showmsg():
    f=open('f2hand.txt', 'r')
    lines = f.readlines()
    strshow = '📣二手装备交易助手\n发布方法(j发二手)\n删除方法(j删二手+序号)\n查看方法(直接发个j二手)\n'
    for line in lines:
        line = line.replace('【','\n【')
        strshow = strshow+'\n'+line
    f.close()
    return strshow

def delnum(delnum,name):
    file = open("f2hand.txt",'r')
    lines = file.readlines()
    n = 1
    with open("f2hand.txt",'w') as f:
        for line in lines:
            num = line[0:1]
            if int(n) == int(delnum):
                if name+' ' in line or name in admin:
                    delnum = False
                else:
                    newline=str(n)+line[1:]
                    f.write(newline)
                    n=n+1
            else:
                newline = str(n)+line[1:]
                f.write(newline) 
                n=n+1
        f.close()

def tohandhelp(act,name,qunnum):
    act = act.replace('\n', '')
    act = act.replace(' ','')
    name=name.replace(' ','')
    qunnum = int(qunnum)
    for key in keys:
        flag = key in act
        flag = flag and flag
    if act=='j二手':
        msgshow=showmsg()
        return msgshow
    if flag:
        if 'j发二手' in act:
            act = act.replace('j发二手','')
            addmsg(act,name,qunnum)
        elif 'j删二手' in act:
            act = act.replace('j删二手','')
            act=int(act)
            delnum(act,name)
        elif 'j清空' in act:
            if name in admin:
                pass
            #clearmsg()
        msgshow = showmsg()
        return msgshow
    else:
        msg='j发二手\n【装备】\【尺码】\n【新旧】\n【价格】\n【联系】'
        return msg

